package org.example.spring_trans.controller;

import org.example.spring_trans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @RequestMapping("/registry")
    public String registry(String name,String password){
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);
        userService.registryUser(name,password);
//        dataSourceTransactionManager.commit(transaction);
        dataSourceTransactionManager.rollback(transaction);
        return "注册成功";

    }
}
